package com.jxb.service;

import com.jxb.protobuf.RPCDateRequest;
import com.jxb.protobuf.RPCDateResponse;
import com.jxb.protobuf.RPCDateServiceGrpc;
import io.grpc.stub.StreamObserver;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class RPCDateServiceImpl extends RPCDateServiceGrpc.RPCDateServiceImplBase {
    @Override
    public void getDate(RPCDateRequest request, StreamObserver<RPCDateResponse> responseObserver) {
        RPCDateResponse rpcDateResponse = null;
        String userName = request.getUserName();
        String response = String.format("你好:%s,今天是%s.", userName, LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

        //定义响应体
        try {
            rpcDateResponse = RPCDateResponse.newBuilder().setServerDate(response).build();
        }finally {
            responseObserver.onNext(rpcDateResponse);
        }
        responseObserver.onCompleted();

    }
}
